﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Runtime.CompilerServices;
using System.Windows.Forms;

namespace APP.HELPER.Utils.Net
{
    public class DownLoadHelper
    {
        public delegate void DownloadStatedEventHander(object sender, string e);
        public delegate void DownloadProcessEventHander(object sender, long length, long position, double percent, double speed);
        public delegate void CancleDownloadEventHander(object sender, string e);
        public delegate void CompleteDownloadEventHander(object sender);
        private DownLoadHelper.CancleDownloadEventHander cancleDownloadEventHander_0;
        private DownLoadHelper.DownloadProcessEventHander downloadProcessEventHander_0;
        private DownLoadHelper.CompleteDownloadEventHander completeDownloadEventHander_0;
        private HttpWebResponse httpWebResponse_0 = null;
        private HttpWebRequest httpWebRequest_0 = null;
        private string string_0 = Application.StartupPath;
        private string string_1 = "";
        private string string_2 = "";
        private long long_0 = 0L;
        private long long_1 = 0L;
        private double double_0 = 0.0;
        private double double_1 = 0.0;
        private long long_2 = 0L;
        private static bool bool_0 = false;
        private int int_0 = 4096;
        private string string_3 = "";
        private string string_4 = "";
        public event DownLoadHelper.CancleDownloadEventHander CancleDownload
        {
            [MethodImpl(MethodImplOptions.Synchronized)]
            add
            {
                this.cancleDownloadEventHander_0 = (DownLoadHelper.CancleDownloadEventHander)Delegate.Combine(this.cancleDownloadEventHander_0, value);
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            remove
            {
                this.cancleDownloadEventHander_0 = (DownLoadHelper.CancleDownloadEventHander)Delegate.Remove(this.cancleDownloadEventHander_0, value);
            }
        }
        public event DownLoadHelper.DownloadProcessEventHander DownloadProcess
        {
            [MethodImpl(MethodImplOptions.Synchronized)]
            add
            {
                this.downloadProcessEventHander_0 = (DownLoadHelper.DownloadProcessEventHander)Delegate.Combine(this.downloadProcessEventHander_0, value);
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            remove
            {
                this.downloadProcessEventHander_0 = (DownLoadHelper.DownloadProcessEventHander)Delegate.Remove(this.downloadProcessEventHander_0, value);
            }
        }
        public event DownLoadHelper.CompleteDownloadEventHander CompleteDownload
        {
            [MethodImpl(MethodImplOptions.Synchronized)]
            add
            {
                this.completeDownloadEventHander_0 = (DownLoadHelper.CompleteDownloadEventHander)Delegate.Combine(this.completeDownloadEventHander_0, value);
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            remove
            {
                this.completeDownloadEventHander_0 = (DownLoadHelper.CompleteDownloadEventHander)Delegate.Remove(this.completeDownloadEventHander_0, value);
            }
        }
        public string Website
        {
            get
            {
                return this.string_4;
            }
            set
            {
                this.string_4 = value;
            }
        }
        public int BlogSize
        {
            get
            {
                return this.int_0;
            }
            set
            {
                this.int_0 = value;
            }
        }
        public string Target
        {
            get
            {
                return this.string_0;
            }
            set
            {
                this.string_0 = value;
            }
        }
        public string Folder
        {
            get
            {
                return this.string_3;
            }
            set
            {
                this.string_3 = value;
            }
        }
        public string Address
        {
            get
            {
                return this.string_1;
            }
            set
            {
                this.string_1 = value;
                this.string_2 = this.string_1.Split(new char[]
				{
					'/'
				})[this.string_1.Split(new char[]
				{
					'/'
				}).Length - 1];
                this.string_0 = this.string_3 + "\\" + this.string_2;
            }
        }
        public string FileName
        {
            get
            {
                return this.string_2;
            }
            set
            {
                this.string_2 = value;
            }
        }
        public long FileSize
        {
            get
            {
                return this.long_0;
            }
        }
        public long Position
        {
            get
            {
                return this.long_1;
            }
        }
        public double Percent
        {
            get
            {
                return this.double_0;
            }
        }
        public double Speed
        {
            get
            {
                return this.double_1;
            }
        }
        public static bool Cancle
        {
            get
            {
                return DownLoadHelper.bool_0;
            }
            set
            {
                DownLoadHelper.bool_0 = value;
            }
        }
        public DownLoadHelper(string URL)
        {
            this.string_2 = URL.Split(new char[]
			{
				'/'
			})[URL.Split(new char[]
			{
				'/'
			}).Length - 1];
            this.string_1 = URL;
            this.string_0 = this.string_0 + "\\" + this.string_2;
            this.string_4 = URL.Substring(7, URL.IndexOf("/", 7) - 7);
        }
        public DownLoadHelper(string URL, string TargetFolder)
        {
            this.string_2 = URL.Split(new char[]
			{
				'/'
			})[URL.Split(new char[]
			{
				'/'
			}).Length - 1];
            this.string_1 = URL;
            this.string_3 = TargetFolder;
            this.string_0 = this.string_3 + "\\" + this.string_2;
        }
        private void method_0(long long_3, long long_4, double double_2, double double_3)
        {
            if (this.downloadProcessEventHander_0 != null)
            {
                this.downloadProcessEventHander_0(this, long_3, long_4, double_2, double_3);
            }
        }
        private void method_1(string string_5)
        {
            if (this.cancleDownloadEventHander_0 != null)
            {
                this.cancleDownloadEventHander_0(this, string_5);
            }
        }
        private void method_2()
        {
            if (this.completeDownloadEventHander_0 != null)
            {
                this.completeDownloadEventHander_0(this);
            }
        }
        public unsafe void Start()
        {
            try
            {
                this.httpWebRequest_0 = (HttpWebRequest)WebRequest.Create(this.string_1);
                this.httpWebResponse_0 = (HttpWebResponse)this.httpWebRequest_0.GetResponse();
            }
            catch (Exception exception)
            {
                this.method_1(exception.Message);
                return;
            }
            this.long_0 = this.httpWebResponse_0.ContentLength;
            this.method_0(this.long_0, this.long_1, this.double_0, this.double_1);
            FileStream stream = new FileStream(this.string_0, FileMode.Create);
            this.long_1 = 0L;
            Stopwatch stopwatch = new Stopwatch();
            this.double_1 = -1.0;
            int i = 0;

            //Label_018D:
            //    *((sbyte*) (voidPtr + 8)) = !bool_0;
            //    if (*(((sbyte*) (voidPtr + 8))) != 0)
            //    {
            //        stopwatch.Start();
            //        byte[] buffer = new byte[this.int_0];
            //        *((int*) (voidPtr + 4)) = this.httpWebResponse_0.GetResponseStream().Read(buffer, 0, this.int_0);
            //        this.long_1 += *((int*) (voidPtr + 4));
            //        this.double_0 = (this.long_1 * 100L) / this.long_0;
            //        this.method_0(this.long_0, this.long_1, this.double_0, this.double_1);
            //        *((sbyte*) (voidPtr + 8)) = *(((int*) (voidPtr + 4))) != 0;
            //        if (*(((sbyte*) (voidPtr + 8))) == 0)
            //        {
            //            this.httpWebResponse_0.GetResponseStream().Close();
            //            stream.Close();
            //            *((sbyte*) (voidPtr + 8)) = !bool_0;
            //            if (*(((sbyte*) (voidPtr + 8))) == 0)
            //            {
            //                File.Delete(this.string_0);
            //                this.method_1("Process abort by User");
            //            }
            //            else
            //            {
            //                this.method_2();
            //            }
            //            return;
            //        }
            //        stream.Write(buffer, 0, *((int*) (voidPtr + 4)));
            //        stopwatch.Stop();
            //        i++;
            //        *((sbyte*) (voidPtr + 8)) = *(((int*) voidPtr)) < 5;
            //        if (*(((sbyte*) (voidPtr + 8))) == 0)
            //        {
            //            this.double_1 = ((double) (this.int_0 * 5)) / (((double) stopwatch.ElapsedMilliseconds) / 1000.0);
            //            stopwatch.Reset();
            //            i = 0;
            //        }
            //        *((sbyte*) (voidPtr + 8)) = 1;
            //        goto Label_018D;
            //    }
            //    this.method_1("Process abort by User");
            //    this.httpWebResponse_0.GetResponseStream().Close();
            //    stream.Close();
        }

    }
}
